package com.wyait.manage.web.biz;

import com.wyait.manage.entity.biz.ShopSearchDTO;
import com.wyait.manage.entity.biz.ShopVO;
import com.wyait.manage.idempotent.annotations.FormPreIdempotent;
import com.wyait.manage.idempotent.annotations.Idempotent;
import com.wyait.manage.pojo.User;
import com.wyait.manage.pojo.biz.Shop;
import com.wyait.manage.service.biz.ShopService;
import com.wyait.manage.utils.PageDataResult;
import com.wyait.manage.utils.SpringSecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huangjian
 */
@Controller
@RequestMapping("/shop")
public class ShopController {
	
	private static final Logger logger = LoggerFactory
			.getLogger(ShopController.class);
	@Autowired
	private ShopService shopService;
	@RequestMapping("/shopList")
	@FormPreIdempotent
	public String toShopList() {
		return "/shop/shopList";
	}
	
	/**
	 * 分页查询商户列表
	 * @return ok/fail
	 */
	@RequestMapping(value = "/getShops",method = RequestMethod.POST)
	@ResponseBody
	@PreAuthorize("hasAnyAuthority('getShops')")
	public PageDataResult getShops(@RequestParam("page") Integer page,
			@RequestParam("limit") Integer limit, ShopSearchDTO shopSearch) {
		logger.debug("分页查询商户列表！搜索条件：shopSearch：" + shopSearch + ",page:" + page
				+ ",每页记录数量limit:" + limit);
		PageDataResult pdr = new PageDataResult();
		try {
			if (null == page) {
				page = 1;
			}
			if (null == limit) {
				limit = 10;
			}
			// 获取商户
			pdr = shopService.getShops(shopSearch, page, limit);
			logger.debug("商户列表查询=pdr:" + pdr);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("商户列表查询异常！", e);
		}
		return pdr;
	}

	
	/**
	 * 设置商户[新增或更新]
	 * @return ok/fail
	 */
	@RequestMapping(value = "/setShop",method = RequestMethod.POST)
	@ResponseBody
	@PreAuthorize("hasAnyAuthority('shopAdd','shopEdit')")
	@Idempotent
	public String setShop(Shop shop) {
		logger.debug("设置商户[新增或更新]！shop:" + shop);
		try {
			if (null == shop) {
				logger.debug("设置商户[新增或更新]，结果=请您填写商户信息");
				return "请您填写商户信息";
			}
			User existUser = SpringSecurityUtils.currentUser();
			if (null == existUser) {
				logger.debug("设置商户[新增或更新]，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			//Thread.sleep(2000);
			// 设置商户[新增或更新]
			logger.info("设置商户[新增或更新]成功！shop=" + shop + "，操作的用户ID=" + existUser.getId());
			return shopService.setShop(shop);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("设置商户[新增或更新]异常！", e);
			return "操作异常，请您稍后再试";
		}
	}

	/**
	 * 删除商户
	 * @return ok/fail
	 */
	@RequestMapping(value = "/delShop", method = RequestMethod.POST)
	@ResponseBody
	@PreAuthorize("hasAnyAuthority('shopDelete')")
	@Idempotent
	public String delShop(@RequestParam("id") Integer id) {
		logger.debug("删除商户！id:" + id);
		String msg = "";
		try {
			if (null == id) {
				logger.debug("删除商户，结果=请求参数有误，请您稍后再试");
				return "请求参数有误，请您稍后再试";
			}
			User existUser = SpringSecurityUtils.currentUser();
			if (null == existUser) {
				logger.debug("删除商户，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			// 删除商户
			Thread.sleep(2000);
			msg = shopService.setDelShop(id, 1);
			logger.info("删除商户:" + msg + "。userId=" + id + "，操作用户id:"+ existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除商户异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 *
	 * @描述：恢复商户
	 * @创建人：wyait
	 * @创建时间：2018年4月27日 上午9:49:14
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/recoverShop", method = RequestMethod.POST)
	@ResponseBody
	@PreAuthorize("hasAnyAuthority('shopDelete')")
	@Idempotent
	public String recoverShop(@RequestParam("id") Integer id) {
		logger.debug("恢复商户！id:" + id);
		String msg = "";
		try {
			User existUser = SpringSecurityUtils.currentUser();
			if (null == existUser) {
				return "您未登录或登录超时，请您登录后再试";
			}
			if (null == id ) {
				return "请求参数有误，请您稍后再试";
			}
			Thread.sleep(2000);
			// 恢复商户
			msg = shopService.setDelShop(id, 0);
			logger.info("恢复商户【" + this.getClass().getName() + ".recoverShop】"
					+ msg + "。商户Id=" + id + "，操作的用户ID=" + existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("恢复商户【" + this.getClass().getName()
					+ ".recoverUser】商户异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 * 查询商户数据
	 * @return map
	 */
	@RequestMapping(value = "/getShop", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getShop(@RequestParam("id") Integer id) {
		logger.debug("查询商户数据！id:" + id);
		Map<String, Object> map = new HashMap<>();
		try {
			if (null == id) {
				logger.debug("查询商户数据==请求参数有误，请您稍后再试");
				map.put("msg", "请求参数有误，请您稍后再试");
				return map;
			}
			// 查询商户
			ShopVO shopvo = shopService.getShop(id);
			logger.debug("查询商户数据！shopvo=" + shopvo);
			if (null != shopvo) {
				map.put("shop", shopvo);
				map.put("msg", "ok");
			} else {
				map.put("msg", "查询商户信息有误，请您稍后再试");
			}
			logger.debug("查询商户数据成功！map=" + map);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "查询商户错误，请您稍后再试");
			logger.error("查询商户数据异常！", e);
		}
		return map;
	}

}
